<?php
/**
 * Created by Adon.
 * User: Adon
 * Date: 2018/3/9
 * Time: 15:53
 */

namespace app\modules\user\controllers\mch;

use app\models\District;
use app\models\DistrictArr;
use app\models\Express;
use app\models\Mch;
use app\models\MchCommonCat;
use app\models\MchPostageRules;
use app\modules\user\behaviors\MchBehavior;
use app\modules\user\behaviors\UserLoginBehavior;
use app\modules\user\controllers\Controller;
use app\modules\user\models\mch\PostageRulesEditForm;
use app\modules\user\models\mch\SettingForm;
use app\modules\user\models\MchDataForm;

class IndexController extends Controller
{
    public $bank = [
        1002=>'工商银行',
        1005=>'农业银行',
        1003=>'建设银行',
        1026=>'中国银行',
        1020=>'交通银行',
        1001=>'招商银行',
        1066=>'邮储银行',
        1006=>'民生银行',
        1010=>'平安银行',
        1021=>'中信银行',
        1004=>'浦发银行',
        1009=>'兴业银行',
        1022=>'光大银行',
        1027=>'广发银行',
        1025=>'华夏银行',
        1056=>'宁波银行',
        4836=>'北京银行',
        1024=>'上海银行',
        1054=>'南京银行',
        4755=>'长子县融汇村镇银行',
        4216=>'长沙银行',
        4051=>'浙江泰隆商业银行',
        4753=>'中原银行',
        4761=>'企业银行（中国）',
        4036=>'顺德农商银行',
        4752=>'衡水银行',
        4756=>'长治银行',
        4767=>'大同银行',
        4115=>'河南省农村信用社',
        4150=>'宁夏黄河农村商业银行',
        4156=>'山西省农村信用社',
        4166=>'安徽省农村信用社',
        4157=>'甘肃省农村信用社',
        4153=>'天津农村商业银行',
        4113=>'广西壮族自治区农村信用社',
        4108=>'陕西省农村信用社',
        4076=>'深圳农村商业银行',
        4052=>'宁波鄞州农村商业银行',
        4764=>'浙江省农村信用社联合社',
        4217=>'江苏省农村信用社联合社',
        4072=>'江苏紫金农村商业银行股份有限公司',
        4769=>'北京中关村银行股份有限公司',
        4778=>'星展银行（中国）有限公司',
        4766=>'枣庄银行股份有限公司',
        4758=>'海口联合农村商业银行股份有限公司',
        4763=>'南洋商业银行（中国）有限公司',
    ];

    public function behaviors()
    {
        return [
            'login' => [
                'class' => UserLoginBehavior::className(),
            ],
            'mch' => [
                'class' => MchBehavior::className(),
            ],
        ];
    }

    public function actionIndex()
    {   
        if (\Yii::$app->request->isAjax) {
            $form = new MchDataForm();
            $form->store_id = $this->store->id;
            $form->mch_id = $this->mch->id;
            $store_data = $form->search();
            return $store_data;
        } else {
            return $this->render('index', [
                'store'=>$this->store
            ]);
        }
    }

    public function actionSetting()
    {
        $mch = $this->mch;
        if (\Yii::$app->request->isPost) {
            $model = \Yii::$app->request->post('model');
            $form = new SettingForm();
            $model['bank_name'] = empty($this->bank[$model['bank_id']])?"":$this->bank[$model['bank_id']];
            $form->model = $mch;
            $form->attributes = $model;
            return $form->save();
        }
        return $this->render('setting', [
            'model'=>$mch,
            'bank'=>$this->bank,
            'province' => DistrictArr::getDistrict(['id' => $mch->province_id]),
            'city' => DistrictArr::getDistrict(['id' => $mch->city_id]),
            'district' => DistrictArr::getDistrict(['id' => $mch->district_id]),
            'mch_common_cat_list' => MchCommonCat::find()->where(['store_id' => $this->store->id, 'is_delete' => 0])->all(),
        ]);
    }

    public function actionPostageRules()
    {
        return $this->render('postage-rules', [
            'list' => MchPostageRules::findAll([
                'mch_id' => $this->mch->id,
                'is_delete' => 0,
            ]),
        ]);
    }

    /**
     * 新增、编辑运费规则
     */
    public function actionPostageRulesEdit($id = null)
    {
        $model = MchPostageRules::findOne([
            'id' => $id,
            'mch_id' => $this->mch->id,
            'is_delete' => 0,
        ]);
        if (!$model) {
            $model = new MchPostageRules();
            $model->mch_id = $this->mch->id;
        }
        if (\Yii::$app->request->isPost) {
            $form = new PostageRulesEditForm();
            $form->attributes = \Yii::$app->request->post();
            $form->postage_rules = $model;
            return $form->save();
        } else {
            $province = DistrictArr::getRules();
            return $this->render('postage-rules-edit', [
                'model' => $model,
                'express_list' => Express::findAll([
                    'is_delete' => 0,
                ]),
                'province_list' => $province,
            ]);
        }
    }

    /**
     * 删除运费规则
     */
    public function actionPostageRulesDelete($id)
    {
        $model = MchPostageRules::findOne([
            'id' => $id,
            'mch_id' => $this->mch->id,
            'is_delete' => 0,
        ]);
        if ($model) {
            $model->is_delete = 1;
            $model->save();
        }
        return [
            'code' => 0,
            'msg' => '删除成功',
        ];
    }

    public function actionPostageRulesSetEnable($id, $type)
    {
        if ($type == 0) {
            MchPostageRules::updateAll(['is_enable' => 0], ['mch_id' => $this->mch->id, 'is_delete' => 0, 'id' => $id]);
        }
        if ($type == 1) {
            MchPostageRules::updateAll(['is_enable' => 0], ['mch_id' => $this->mch->id, 'is_delete' => 0]);
            MchPostageRules::updateAll(['is_enable' => 1], ['mch_id' => $this->mch->id, 'is_delete' => 0, 'id' => $id]);
        }
        $this->redirect(\Yii::$app->request->referrer)->send();
    }

    public function actionPassword()
    {

        $id = \Yii::$app->request->get("user_id");
        $password= \Yii::$app->request->get("password");
        $mch = Mch::findOne(['user_id'=>$id]);
        if($password)   $mch->password =  \Yii::$app->security->generatePasswordHash($password, 5);
        $mch->save();
        return [
            'code' => 0,
            'data' => "",
            'msg'=>"修改成功"
        ];
    }
}
